//
// Created by Scave on 2024/2/24.
//

#ifndef RIDER3D_MESHUTIL_H
#define RIDER3D_MESHUTIL_H

#include "Define.h"
#include "component/BasicComponents.h"

NS_RIDER_BEGIN

    class MeshUtil {
    public:
        static void BuildRectangle(Mesh* mesh,
                                   float width, float height, Color color = kDefaultRectColor);

        static void BuildTriangle(Mesh* mesh,
                                  float width, float height, Color color = kDefaultRectColor);

        static void BuildBox(Mesh* mesh,
                             float width, float height, float depth, Color colors[6] = const_cast<Color*>(kDefaultBoxColors));

        static void BuildSphere(Mesh* mesh, uint16_t sectors, uint16_t stacks, float radius, Color color = kDefaultRectColor);

        static void BuildSkyBox(Mesh* mesh, float size, float quality);

        static void BuildCapsule(Mesh* mesh, uint16_t sectors, uint16_t stacks, float radius, float length, Color color = kDefaultRectColor);

        static void BuildCylinder(Mesh* mesh, float radius_outer, float radius_inner,
            float height, uint16_t sectors, uint16_t stacks, Color color = kDefaultRectColor);

        static void BuildTorus(Mesh* mesh,
                               float large_radius, float small_radius, int segments_w, int segments_h);

        static void BuildTriangularPyramid(Mesh* mesh,
                                           float width, float height, float depth, Color color[4] = const_cast<Color*>(kDefaultTriangularPyramidColor));
        
        static void FindMinMaxXYZ(Mesh* mesh, Vec3& min, Vec3& max);

    private:
        const static Color kDefaultRectColor;
        const static Color kDefaultBoxColors[6];
        const static Color kDefaultTriangularPyramidColor[4];
        const static Color kDefaultBackColor;
        
        enum class SphereType {
            All,     // 完整球体
            Top,     // 上半球
            Bottom   // 下半球
        };

        static void BuildSphereImpl(Mesh* mesh, uint16_t sectors, uint16_t stacks, float radius, float y_move = 0,
                                    SphereType type = SphereType::All, Color color = kDefaultRectColor);
        
        static int ClockOrder(const Mesh* mesh, uint16_t idx1, uint16_t idx2, uint16_t idx3, Vec3 normal = {0, 0, 1});

    };

NS_RIDER_END

#endif //RIDER3D_MESHUTIL_H
